define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);

                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    var content = {
                        tool_no: barcode
                    };
                    tool_info_get(content,function(data){
                        // P 錫膏 ||  A 紅膠
                        if(data.tool_class == '0' || data.tool_class == '3'){
                            $scope.type_no = 'Type1';
                            $scope.tool_type_no = data.tool_type_no;
                            $scope.tool_class = data.tool_class;
                            $scope.tool_no = data.tool_no;
                            $scope.tool_state = data.state;
                            $scope.State_Start_Time = data.state_start_time;
                            $scope.opened_limit_time = data.state_start_time + data.opened_limit_time;
                            $scope.machine_no = data.equipment_no;
                            tool_handle_set_process({
                                tool_details: [
                                    {
                                        equipment_no: $scope.machine_no,
                                        tooltype_no: $scope.tool_type_no,
                                        tool_type: $scope.tool_class,
                                        tool_no: $scope.tool_no,
                                        tool_handle_type: '-1'
                                    }
                                ]
                            },function(data){
                                if(data && data.length > 0)
                                {
                                    if(data[0].status == 'E')
                                        $scope.$root.showAlert(data[0].tool_no + data[0].result);
                                    else
                                        $scope.$root.showToast($filter('translate')('SMT09.msg.success'));
                                } 
                                else
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            });
                        // Q 刮刀 || S 網板 || X 其他
                    } else if (data.tool_class == '2' || data.tool_class == '1' || data.tool_class == '4'){
                            $scope.type_no = 'Type2';
                            $scope.tool_class = data.tool_class;
                            $scope.tool_no = data.tool_no;
                            $scope.tool_state = data.state;
                            $scope.life_count = data.life_count,
                            $scope.acc_count = data.acc_count,
                            $scope.usage_count = data.usage_count,
                            $scope.actual_count = data.actual_count,
                            $scope.last_check_date = data.last_check_date;
                            $scope.last_mantain_date = data.last_mantain_date;
                            $scope.machine_no = data.equipment_no;
                            var content = 
                            { 
                                tool_details:[
                                {
                                    equipment_no: $scope.machine_no,
                                    tooltype_no: $scope.tool_type_no,
                                    tool_type: $scope.tool_class,
                                    tool_no: $scope.tool_no,
                                    tool_handle_type: '-1',
                                    actual_count: $scope.actual_count,
                                    acc_count: $scope.acc_count
                                }
                            ]};
                            tool_handle_set_process(content,function(data){
                                if(data && data.length > 0)
                                {
                                    if(data[0].status == 'E')
                                        $scope.$root.showAlert(data[0].tool_no + data[0].result);
                                    else
                                        $scope.$root.showToast($filter('translate')('SMT09.msg.success'));
                                } 
                                else
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            });
                        } else {
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        };
                    });
                };
            };

            // 取回 工具資訊
            function tool_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 送出資料(只會回傳一筆)
            function tool_handle_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.tool_handle_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
